package com.ny.zmb.openapi.controller;

import com.alibaba.fastjson.JSONObject;
import com.ny.zmb.glxt.service.PmStoreExtService;
import com.ny.zmb.openapi.controller.entity.Result;
import com.ny.zmb.openapi.entity.PmApiLog;
import com.ny.zmb.openapi.entity.PmUserToken;
import com.ny.zmb.openapi.service.IPmApiLogService;
import com.ny.zmb.openapi.service.IPmUserTokenService;
import com.ny.zmb.openapi.service.PmRegisterService;
import com.ny.zmb.openapi.util.OSSUtil;
import com.ny.zmb.openapi.ysutil.YsHttpUtil;
import com.ny.zmb.openapi.ysutil.base.EYinShengBase;
import com.ny.zmb.openapi.ysutil.common.ServiceEnum;
import com.ny.zmb.openapi.ysutil.common.YsPayConstant;
import com.ny.zmb.openapi.ysutil.from.*;
import com.ny.zmb.openapi.ysutil.from.BizReqJsonCollege.UploadDocumentReqCommandPlus;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

import static com.ny.zmb.openapi.ysutil.utils.Base64Utils.encodeImageToBase64;

@CrossOrigin
@RequestMapping("/ys/common")
@RestController
public class YsCommonController extends EYinShengBase {


    @Resource
    private YsHttpUtil ysHttpUtil;
    @Resource
    private OSSUtil ossUtil;
    @Resource
    private PmRegisterService pmRegisterService;
    @Resource
    private PmStoreExtService pmStoreExtService;
    @Resource
    private IPmUserTokenService pmUserTokenService;
    @Resource
    private IPmApiLogService pmApiLogService;

    /**
     * YS图片上传专用
     *
     * @param obj
     * @param file
     * @return
     * @throws Exception
     */
    @PostMapping(value = "registerForFileUpdate", consumes = "multipart/form-data")
    public Result registerForFileUpdate(UploadDocumentReqCommandPlus obj, MultipartFile file) {

        pmApiLogService.save(new PmApiLog("ys/register" + "/" + "registerForFileUpdate", obj.toString()));
        String ossPath = pmRegisterService.fileUploadForOss(file, file.getOriginalFilename());
        if (ossPath == null || ossPath.isEmpty()) {
            return Result.fail("OSS图片上传失败");
        }
        // 优化上传
        String ysPath = pmRegisterService.filePathUploadForYs(file, ossPath, obj);
        if (ysPath == null || ysPath.isEmpty()) {
            return Result.fail("YS图片上传失败");
        }
        PmUserToken userDetailByToken = getUserToken();
        Integer userId = userDetailByToken.getUserId();
        Boolean flag1 = pmStoreExtService.infoUpdate(userId, obj, ossPath);
        Boolean flag2 = pmRegisterService.registerForFileUpdate(userId, obj, ysPath);
        JSONObject result = new JSONObject();
        if (flag1 && flag2) {
            result.put("msg", "外部商户号:" + obj.getOutMerchantNo() + " - " + obj.getImgType() + "更新成功");
            result.put("fileCode", ysPath);
            result.put("ossUrl", ossPath);
            return Result.ok(result);
        } else {
            result.put("msg", "外部商户号:" + obj.getOutMerchantNo() + " - " + obj.getImgType() + "更新失败");
            return Result.fail(result);
        }
    }

    @PostMapping(value = "/ossFileUpdate", consumes = "multipart/form-data")
    public Result ossForFileUpdate(MultipartFile file) {
        String ossPath = pmRegisterService.fileUploadForOss(file, file.getOriginalFilename());
        return Result.ok(ossPath);
    }

    @PostMapping(value = "/ossUpload", consumes = "multipart/form-data")
    public Result ossUpload(MultipartFile file, String path) {
        String ossPath = pmRegisterService.ossUpload(file, file.getOriginalFilename(), path);
        return Result.ok(ossPath);
    }


    @PostMapping(value = "/ysFileUpdate", consumes = "multipart/form-data")
    public Result ysForFileUpdate(MultipartFile file) {
        String ossPath = pmRegisterService.fileUploadForOss(file, file.getOriginalFilename());
        String ysPath = pmRegisterService.filePathUploadForYs(file, ossPath, null);
        return Result.ok(ysPath);
    }

    /**
     * 同一上传文件
     *
     * @param command
     * @return
     */
    @PostMapping("upload")
    public Result upload(@RequestBody UploadDocumentReqCommand command) {
        command.setFileBase64(encodeImageToBase64(command.getFilePath()));
        try {
            if (null == command) {
                return Result.fail("参数不能为空");
            }
            JSONObject send = ysHttpUtil.send(ServiceEnum.PIC_UPLOAD, YsPayConstant.ENV_PRD,
                    MERCHANT_PRX, MERCHANT_PRX_PWD, ServiceEnum.PIC_UPLOAD.getApiName(), command);
            JSONObject result = send.getJSONObject("bizResponseJson");
            return Result.ok(result);
        } catch (Exception var) {
            return Result.fail(var.getMessage());
        }
    }

    /**
     * 查询mcc码
     *
     * @param command
     * @return
     */
    @GetMapping("queryMccList")
    public Result queryMccList(QueryMccListCommand command) {
        try {
            if (null == command) {
                return Result.fail("参数不能为空");
            }
            JSONObject send = ysHttpUtil.send(ServiceEnum.Query_MccList, YsPayConstant.ENV_PRD,
                    MERCHANT_PRX, MERCHANT_PRX_PWD, ServiceEnum.Query_MccList.getApiName(), command);
            JSONObject result = send.getJSONObject("bizResponseJson");
            return Result.ok(result);
        } catch (Exception var) {
            return Result.fail(var.getMessage());
        }
    }

    /**
     * 查询mcc码
     *
     * @param command
     * @return
     */
    @GetMapping("modifyMobile")
    public Result modifyMobile(ModifyMobileCommand command) {
        try {
            if (null == command) {
                return Result.fail("参数不能为空");
            }
            JSONObject send = ysHttpUtil.send(ServiceEnum.MODIFY_MOBILE, YsPayConstant.ENV_PRD,
                    MERCHANT_PRX, MERCHANT_PRX_PWD, ServiceEnum.MODIFY_MOBILE.getApiName(), command);
            JSONObject result = send.getJSONObject("bizResponseJson");
            return Result.ok(result);
        } catch (Exception var) {
            return Result.fail(var.getMessage());
        }
    }


    /**
     * 地区查询
     *
     * @param command
     * @return
     */
    @GetMapping("queryAreaList")
    public Result queryAreaList(QueryAreaListCommand command) {
        try {
            if (null == command) {
                return Result.fail("参数不能为空");
            }
            JSONObject send = ysHttpUtil.send(ServiceEnum.Query_AreaList, YsPayConstant.ENV_PRD,
                    MERCHANT_PRX, MERCHANT_PRX_PWD, ServiceEnum.Query_AreaList.getApiName(), command);
            JSONObject result = send.getJSONObject("bizResponseJson");
            return Result.ok(result);
        } catch (Exception var) {
            return Result.fail(var.getMessage());
        }
    }

    /**
     * 开户行查询
     *
     * @param command
     * @return
     */
    @GetMapping("queryOpenBankArea")
    public Result queryOpenBankArea(QueryOpenBankAreaCommand command) {
        try {
            if (null == command) {
                return Result.fail("参数不能为空");
            }
            JSONObject send = ysHttpUtil.send(ServiceEnum.QueryOpenBankArea, YsPayConstant.ENV_PRD,
                    MERCHANT_PRX, MERCHANT_PRX_PWD, ServiceEnum.QueryOpenBankArea.getApiName(), command);
            JSONObject result = send.getJSONObject("bizResponseJson");
            return Result.ok(result);
        } catch (Exception var) {
            return Result.fail(var.getMessage());
        }
    }
}
